package labox.innovation.gameserver.network.clientpackets;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import labox.innovation.config.Config;
import labox.innovation.gameserver.datatables.SkillTable;
import labox.innovation.gameserver.model.FuzeWorld;
import labox.innovation.gameserver.model.L2Object;
import labox.innovation.gameserver.model.L2Skill;
import labox.innovation.gameserver.model.actor.L2Attackable;
import labox.innovation.gameserver.model.actor.L2Character;
import labox.innovation.gameserver.model.actor.instance.FuzePcInstance;
import labox.innovation.gameserver.network.SystemMessageId;
import labox.innovation.gameserver.network.serverpackets.ActionFailed;
import labox.innovation.gameserver.network.serverpackets.SystemMessage;
import labox.innovation.util.Point2D;

/**
 *  客户端使用技能协议
 * @author zjj
 *
 */
public final class RequestMagicSkillUse extends L2GameClientPacket {

	private static final String _C__56_REQUESTMAGICSKILLUSE = "[C] 0x56 RequestMagicSkillUse";

	private static final Logger _log = LoggerFactory.getLogger(RequestMagicSkillUse.class.getName());

	//技能id
	private int _magicId;

	//对象uid
	private long _objId;

	@Override
	protected void readImpl() {
		_magicId = readInt(); // Identifier of the used skill  

		if (_log.isDebugEnabled())
			_log.debug("技能id : {}", _magicId);

		_objId = readLong();

	}

	@Override
	protected void runImpl() {
		// Get the current L2PcInstance of the player
		FuzePcInstance activeChar = getClient().getActiveChar();

		if (activeChar == null)
			return;
		
		if(activeChar.isSpawnProtected())
			activeChar.setProtection(false);

		L2Object target = null;

		//		Point2D worldPosition = new Point2D( activeChar.getX(), activeChar.getY() );
		//		activeChar.setCurrentSkillWorldPosition( worldPosition );

		//目标是对象

		if( activeChar.getObjectId() == _objId ){
			target = activeChar;
		}else{
			target = activeChar.getKnownList().getKnownCharacters().get(_objId);
		}

		if (target == null){
			if( _log.isDebugEnabled() )
				_log.debug("目标不存在");
			return;
		}

		Point2D worldPosition = new Point2D(target.getX(), target.getY());
		activeChar.setCurrentSkillWorldPosition(worldPosition);

		if( _log.isDebugEnabled() ){
			_log.debug("设置目标:" + target.getName() + "目标uid：" + target.getObjectId() );
		}
		
		activeChar.setTarget(target);
		

		// 获得指定技能的等级
		// 只有这个技能存在 人物技能里 才可能使用这个技能
		int level = activeChar.getSkillLevel(_magicId);
		//		level = 1;// Only for Test. 永远使用一级技能
		if (level <= 0) {
			if( _log.isDebugEnabled() )
				_log.debug("角色身上没有这个技能: {} " , _magicId);
			return;
		}

		//		System.out.println( target + "移动速度:" + ( (L2Character)target ).getRunSpeed() );

		// Get the L2Skill template corresponding to the skillID received from the client
		L2Skill skill = SkillTable.getInstance().getInfo(_magicId, level);
		
		// 如果不能攻击此玩家 那么直接返回
		if( activeChar.getObjectId() != target.getObjectId() && skill.isOffensive() && !(target instanceof L2Attackable) && !target.isAutoAttackable(activeChar) ){
//			_log.info(" 不能攻击新手 " );
//			sendPacket(new SystemMessage(SystemMessageId.YOU_ARE_IN_NEW_PLAYER_TIME));
			activeChar.sendMessage("不能攻击目标");
			return;
		}
		
		//如果是buff技能 那么不能给不能被攻击的目标加， 也不可以给非友善目标加
		if( activeChar.getObjectId() != target.getObjectId() && !skill.isOffensive() && !skill.isDebuff() && target.isAttackable() ){
			
			if( target.isAutoAttackable(activeChar) ){
				sendPacket(new SystemMessage(SystemMessageId.TARGET_IS_INCORRECT));
				if( _log.isDebugEnabled() )
					_log.debug("buff技能 不能给敌对玩家释放 " );
				return;
			}
			
		}

		// Check the validity of the skill
		if (skill != null) {
			if ((activeChar.isTransformed() || activeChar.isInStance()) && !activeChar.containsAllowedTransformSkill(skill.getId())) {
				if (_log.isDebugEnabled())
					_log.debug("变形不允许使用的技能");
				activeChar.sendPacket(ActionFailed.STATIC_PACKET);
				return;
			}

			if (_log.isDebugEnabled())
				_log.debug("{}使用技能:{}",activeChar,skill);
				
			// players mounted on pets cannot use any toggle skills
			if (activeChar.isMounted())
				return;
			// activeChar.stopMove();
			activeChar.useMagic(skill, false, false);
		} else {
			activeChar.sendPacket(ActionFailed.STATIC_PACKET);
			_log.warn("No skill found with id " + _magicId + "and level !!" + level, Config.SERVER_ID );
		}
	}

	/* (non-Javadoc)
	 * @see labox.innovation.gameserver.clientpackets.ClientBasePacket#getType()
	 */
	@Override
	public String getType() {
		return _C__56_REQUESTMAGICSKILLUSE;
	}

	protected boolean triggersOnActionRequest() {
		return true;
	}
}
